import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

chrome_options = Options()
chrome_options.add_argument("--headless")

# base_url = "http://www.baidu.com/"
base_url = "https://weibo.com/"
# 对应的chromedriver的放置目录
driver = webdriver.Chrome(chrome_options=chrome_options)


def login(base_url):
    driver.get(base_url)
    driver.set_window_size("1649", "2535")

    # 等待指定的标签
    element = WebDriverWait(driver, 5, 0.5).until(
        EC.presence_of_element_located((By.ID, "loginname"))
    )
    element.send_keys("lixuweiok@sina.com")
    driver.save_screenshot('screen.png')


start_time = time.time()
print('this is start_time ', start_time)
try:
    login(base_url)

    js = "window.scrollTo(0,450)"
    driver.execute_script(js)
    time.sleep(3)

except BaseException as msg:
    print(msg)
finally:
    # 关闭所有的window
    driver.quit()

end_time = time.time()
print('this is end_time ', end_time)
